package com.cmcc.aregister.ui.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.util.PotoalParcelable;
import com.cmcc.aregister.ui.adapter.AddressAdapter;

/**
 * 名院筛选
 * @author Administrator
 * 
 */
public class ScreeningActivity extends ParentActivity {
    // ------view------
    private Button top_btn;
    // private TextView top_left;
    // private TextView top_right;
    private LinearLayout content_layout;
    private TextView top_text;
    private ListView top_listView;

    // ------parameter------
    private int currentTitleSelect = -1;
    private int type;
    private int title;
    private String[] local_address_number;
    private String[] province_address_number;
    private String[] subject_name_str;
    private String[] content;
    private int top_text_str;

    // -----------
    private AddressAdapter addressAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(R.layout.p_screening);
        local_address_number = getResources().getStringArray(
            R.array.local_address_number);
        province_address_number = getResources().getStringArray(
            R.array.province_address_number);
        subject_name_str = getResources().getStringArray(
            R.array.subject_name_str);
    }

    @Override
    protected void setupData() {
        Map<String, Object> httpMap = ((PotoalParcelable) getIntent()
            .getParcelableExtra(Arguments.INTENT_TO_NEXT_PAGE)).map;
        type = (Integer) httpMap.get(Arguments.ACTIVITY_JUMP_SCREENING_TYPE);
        switch (type) {
        case 0:
            title = R.string.hospital_screening;
            content = getResources().getStringArray(R.array.local_address_name);
            top_text_str = R.string.according_to_the_regional;
            break;
        case 1:
            title = R.string.subject_screening;
            content = getResources().getStringArray(R.array.subject_name);
            top_text_str = R.string.according_to_the_subject;
            break;
        case 2:
            title = R.string.doctor_screening;
            content = getResources().getStringArray(R.array.subject_name);
            top_text_str = R.string.according_to_the_subject;
            break;
        }
    }

    @Override
    protected void setupView() {
        ((ImageView) findViewById(R.id.title))
            .setBackgroundResource(R.drawable.mingyishaixuan);
        top_btn = (Button) findViewById(R.id.top_right_btn);
        // top_left = (TextView) findViewById(R.id.left_btn);
        // top_right = (TextView) findViewById(R.id.right_btn);
        content_layout = (LinearLayout) findViewById(R.id.content_layout);
        top_text = (TextView) findViewById(R.id.top_text);
        top_listView = (ListView) findViewById(R.id.top_listview);

        top_btn.setText(R.string.finish);
        top_btn.setTextColor(getResources().getColor(R.color.sky_blue));
        top_btn.setTextSize(18);
        top_btn
            .setBackgroundColor(getResources().getColor(R.color.transparent));
        top_btn.setVisibility(View.VISIBLE);
        top_btn.setOnClickListener(onClickListener);
        // top_left.setOnClickListener(onClickListener);
        // top_right.setOnClickListener(onClickListener);
        top_listView.setOnItemClickListener(onItemClickListener);
        check(R.id.left_btn, false);
    }

    OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
            addressAdapter.checkPosition = position;
            addressAdapter.notifyDataSetChanged();
        }
    };

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.top_right_btn) {
                Intent intent = new Intent(ScreeningActivity.this,
                    ScreeningResultActivity.class);
                Map<String, Object> httpMap = new HashMap<String, Object>();
                httpMap.put(Arguments.ACTIVITY_JUMP_SCREENING_TYPE, type);
                if (currentTitleSelect == R.id.left_btn) {
                    httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE, "0");
                    if (type == 0) {
                        httpMap.put(Arguments.ACTIVITY_JUMP_AREA_ID,
                            local_address_number[addressAdapter.checkPosition]);
                    }
                    else {
                        httpMap.put(Arguments.ACTIVITY_JUMP_CAT_NO,
                            subject_name_str[addressAdapter.checkPosition]);
                    }

                }
                else {
                    httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE, "1");
                    if (type == 0) {
                        httpMap
                            .put(
                                Arguments.ACTIVITY_JUMP_AREA_ID,
                                province_address_number[addressAdapter.checkPosition]);
                    }
                    else {
                        httpMap.put(Arguments.ACTIVITY_JUMP_CAT_NO,
                            subject_name_str[addressAdapter.checkPosition]);
                    }
                }
                PotoalParcelable potoal = new PotoalParcelable(httpMap);
                intent.putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
            else {
                check(v.getId(), true);
            }
        }
    };

    /**
     * 
     * @param id
     * @param hasAnimation 是否需要动画
     */
    public void check(int id, boolean hasAnimation) {
        if (currentTitleSelect == id) {
            return;
        }
        if (currentTitleSelect > 0) {
            // 屏蔽异地预约
            if (currentTitleSelect == R.id.left_btn) {
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.yidi_no_use), getString(R.string.close));
                return;
            }
            findViewById(currentTitleSelect).setSelected(false);
        }
        currentTitleSelect = id;
        findViewById(currentTitleSelect).setSelected(true);
        if (hasAnimation) {
            start3DAnimation(content_layout);
        }
        else {
            addressAdapter = new AddressAdapter(context, content);
            top_text.setText(top_text_str);
            top_listView.setAdapter(addressAdapter);
        }
    }

    @Override
    public void animFrushView() {
        addressAdapter.checkPosition = 0;
        if (type == 0) {
            if (currentTitleSelect == R.id.left_btn) {
                content = getResources().getStringArray(
                    R.array.local_address_name);
                top_text_str = R.string.according_to_the_regional;
            }
            else {
                content = getResources().getStringArray(
                    R.array.province_address_name);
                top_text_str = R.string.according_to_the_pro_and_city;
            }
        }
        top_text.setText(top_text_str);
        addressAdapter.first = content;
        addressAdapter.notifyDataSetChanged();
    }
}